/*
Takes all anon-t-s of age 20-50 in years 2009-2017 in which the person has EMP=1 (or l.EMP=1) and has a BNO S code
Assigns the BNO S categories as well as a single one (first of the originally considered Sext if any, or first of all S)
Defines the number of months being absent after the accident (including m=0 if EMP=0 but l.EMP=1)
	Runs from 0 (no absence) to 13 (at least 13 months of absence with a known date of return)
	Missing if date of return not known
*/

use anon t vallazon1 ev ho w1 kor ferfi fogvisz1 feor1 wh1 transfer_mtp id_tip wtip1 jnap if (kor>=20 & kor<=50) & (ev>=2009 & ev<=2017) ///
	using "$in/admin3_alap.dta", clear
merge 1:1 anon t using "$in/admin3_passziv.dta", keepusing(passziv_tip1) keep(master match) nogen
rename passziv_tip1 SLdays

* information on EMP status	
gen EMP = (vallazon1!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (SLdays<6 | SLdays==.) & (fogvisz1==201)
	
* occupation *
cap drop occupation
gen occupation=. 
replace occupation=0 if missing(feor1_h2)
replace occupation=3 if feor1_h2==11
replace occupation=4 if feor1_h2==12
replace occupation=7 if feor1_h2==13 
replace occupation=1 if feor1_h2==14
replace occupation=1 if feor1_h2==15
replace occupation=3 if feor1_h2>=17 & feor1_h2<=23
replace occupation=4 if feor1_h2>=24 & feor1_h2<=33
replace occupation=5 if feor1_h2>=34 & feor1_h2<=43
replace occupation=6 if feor1_h2>=44 & feor1_h2<=47
replace occupation=7 if feor1_h2==48

#delimit ;
lab def occupation
0 "No info"
1 "Manager, political/religious/ngo leader"
3 "Professional"
4 "Other white collar"
5 "Skilled blue collar"
6 "Assembler, machine op"
7 "Unskilled laborer", replace
;
#delimit cr
lab val occupation occupation 

cap drop _merge
merge 1:1 anon t using "$out/bnoSext_merge_allS.dta"
drop if _m==2
drop _merge
sort anon t

recode max_bnoSext (.=0)
recode max_bnoSall (.=0)

merge 1:1 anon t using "$in/admin3_eu_fekvo.dta", ///
keep(master match) nogen keepusing(korhazi_nap) 

gen hosp5 = 1 if korhazi_nap>=5 & korhazi_nap~=.
	recode hosp5 (.=0)

xtset anon t	
	
gen ACC=max_bnoSext==1 & (EMP==1 | l.EMP==1)
gen ACCall=max_bnoSall==1 & (EMP==1 | l.EMP==1)

gen d=0 if ACCall==1
replace d=1 if d==0 & ((EMP==1 & f.EMP==0) | (l.EMP==1 & EMP==0))
replace d=2 if d==1 & ((EMP==1 & f2.EMP==0) | (EMP==0 & f.EMP==0))
foreach x of numlist 3/13{
	local y=`x'-1
	replace d=`x' if d==`y' & ((EMP==1 & f`x'.EMP==0) | (EMP==0 & f`y'.EMP==0))
}
egen maxempt=max(t) if EMP==1, by(anon)
gen noreturn=1 if ACCall==1 & f.EMP!=1 & maxempt<=t
tab d noreturn, m
egen maxt=max(t), by(anon)
assert maxt==t+d if d<=12 & noreturn==1
assert maxt>=t+13 if d==13 & noreturn==1
replace d=. if noreturn==1

assert d==. if ACCall!=1
assert d==0 if ACCall==1 & EMP==1 & f.EMP==1
assert d==1 if ACCall==1 & EMP==1 & f.EMP==0 & f2.EMP==1
assert d==1 if ACCall==1 & EMP==0 & f.EMP==1
assert d==2 if ACCall==1 & EMP==1 & f.EMP==0 & f2.EMP==0 & f3.EMP==1
assert d==2 if ACCall==1 & EMP==0 & f.EMP==0 & f2.EMP==1
assert d==3 if ACCall==1 & EMP==1 & f.EMP==0 & f2.EMP==0 & f3.EMP==0 & f4.EMP==1 
assert d==3 if ACCall==1 & EMP==0 & f.EMP==0 & f2.EMP==0 & f3.EMP==1
assert d==4 if ACCall==1 & EMP==1 & f.EMP==0 & f2.EMP==0 & f3.EMP==0 & f4.EMP==0 & f5.EMP==1
assert d==4 if ACCall==1 & EMP==0 & f.EMP==0 & f2.EMP==0 & f3.EMP==0 & f4.EMP==1

keep if ACCall==1

* this D is defined in a different way than the one we use, it corresponds to d-1
rename d D
label var D "d-1=# absent months,13 if more than 12,. if no return"

drop ACCall max_bnoSext max_bnoSall
label var ACC "original sample of treated with Sext BNOs"

* assign the first BNO Sext (from the originally used list) code or the first of all BNO S if no such

gen BNOcode=""
label var BNOcode "single BNO S assigned (from Sext if any)"
foreach x in S03 S13 S23 S33 ///
	S40 S41 S42 S43 S44 S45 S46 S49 ///
	S50 S51 S52 S53 S54 S55 S56 S59 ///
	S60 S61 S62 S63 S64 S65 S66 S69 ///
	S70 S71 S72 S73 S74 S75 S76 S79 ///
	S80 S81 S82 S83 S84 S85 S86 S89 ///
	S90 S91 S92 S93 S94 S95 S96 S99 ///
  {
  	replace BNOcode="`x'" if BNOcode=="" & BNO`x'==1
}
foreach x in S00 S01 S02 S04 S05 S06 S07 S08 S09 ///
	S10 S11 S12 S14 S15 S16 S17 ///
	S20 S21 S22 S24 S25 S26 S27 S28 S29 ///
	S30 S31 S32 S34 S35 S36 S37 S38 ///
	S47 S48 S58 S67 S68 S78 S88 S98 ///
  {
  	replace BNOcode="`x'" if BNOcode=="" & BNO`x'==1
}
codebook BNOcode

tab BNOcode D, m

sum
save "$out/bnoSall_with_d.dta", replace
